#include "StdAfx.h"
#include "DrawBitmap.h"
namespace UILib
{
    CDrawBitmap::CDrawBitmap(void) : CDrawView( NULL, -1, NULL )\
        , pBitMap_( NULL )\
        , eType_( Draw_FixSize )
    {
        memset( &pSplit_, 0, sizeof(SplitPos) );
    }

    CDrawBitmap::CDrawBitmap( Bitmap* pBitmap , const PUIRect pRect
                                ,  const PSplitPos pSplit/* = NULL */, const eDrawType eType /*= Draw_FixSize*/
                                , const UINT uID /*= -1*/ , ViewListener* pListener /*= NULL */ 
                                )
        : CDrawView( pRect, uID, pListener )\
        , pBitMap_(pBitmap)\
        , eType_(eType)\

    {
        if ( pSplit )
        {
            memcpy(&pSplit_, pSplit, sizeof(SplitPos) );
        }
        else
        {
            memset( &pSplit_, 0, sizeof(SplitPos) );
        }
    }

    CDrawBitmap::~CDrawBitmap( void )
    {
    }

    void CDrawBitmap::DrawElement( const HDC hDC, const RECT &rcPaint )
    {
        CDrawView::bNeedInvalidate_ = false;
        if ( pBitMap_ == NULL || !IsVisible() )
        {
            return;
        }
        int nW = pBitMap_->GetWidth();
        int nH = pBitMap_->GetHeight();

        RECT rcEmem = { 0 };
        CDrawView::GetRECT(rcEmem);

        Graphics graph(hDC);
        if ( eType_ == Draw_FixSize )
        {
            Rect rcDest(rcEmem.left, rcEmem.top, nW, nH );
            graph.DrawImage(pBitMap_, rcDest, 0, 0, nW, nH, UnitPixel, NULL, NULL, NULL);
        }
        else if ( eType_ == Draw_FillRect )
        {
            SIZE szElem = { rcEmem.right - rcEmem.left, rcEmem.bottom - rcEmem.top };
            Rect rcDest(rcEmem.left, rcEmem.top, szElem.cx, szElem.cy );
            graph.DrawImage(pBitMap_, rcDest, 0, 0, nW, nH, UnitPixel, NULL, NULL, NULL);
        }
        else if ( eType_ == Draw_ThreeInOneH )
        {
            //Draw Bkg
            SIZE szElem = { rcEmem.right - rcEmem.left, rcEmem.bottom - rcEmem.top };
            Rect rcBkgDestL(rcEmem.left, rcEmem.top, pSplit_.lBegin, szElem.cy );
            Rect rcBkgDestM(rcEmem.left + pSplit_.lBegin, rcEmem.top, szElem.cx - ( nW - (pSplit_.lEnd - pSplit_.lBegin) ), szElem.cy );
            Rect rcBkgDestR(rcEmem.right - ((nW) - pSplit_.lEnd), rcEmem.top, (nW) - pSplit_.lEnd, szElem.cy );

            graph.DrawImage(pBitMap_, rcBkgDestL, 0, 0, pSplit_.lBegin, nH, UnitPixel, NULL, NULL, NULL);
            graph.DrawImage(pBitMap_, rcBkgDestM, pSplit_.lBegin,0,  pSplit_.lEnd - pSplit_.lBegin, nH, UnitPixel, NULL, NULL, NULL);
            graph.DrawImage(pBitMap_, rcBkgDestR, pSplit_.lEnd, 0, nW - pSplit_.lEnd, nH, UnitPixel, NULL, NULL, NULL);
        }
        else if ( eType_ == Draw_ThreeInOneV )
        {
            //Draw Bkg
            SIZE szElem = { rcEmem.right - rcEmem.left, rcEmem.bottom - rcEmem.top };
            Rect rcBkgDestT(rcEmem.left, rcEmem.top, szElem.cx, pSplit_.lBegin );
            Rect rcBkgDestM(rcEmem.left, rcEmem.top + pSplit_.lBegin, szElem.cx, szElem.cy -(nH - (pSplit_.lEnd -  pSplit_.lBegin )) );
            Rect rcBkgDestB(rcEmem.left, rcEmem.top + szElem.cy - (nH - pSplit_.lEnd), szElem.cx, nH - pSplit_.lEnd );

            graph.DrawImage(pBitMap_, rcBkgDestT, 0, 0, nW, pSplit_.lBegin, UnitPixel, NULL, NULL, NULL);
            graph.DrawImage(pBitMap_, rcBkgDestM, 0,pSplit_.lBegin,  nW, (pSplit_.lEnd -  pSplit_.lBegin ), UnitPixel, NULL, NULL, NULL);
            graph.DrawImage(pBitMap_, rcBkgDestB,  0, pSplit_.lEnd, nW, nH - pSplit_.lEnd, UnitPixel, NULL, NULL, NULL);
        }
    }

    void CDrawBitmap::SetBitmap( Bitmap* pBitmap )
    {
        pBitMap_ = pBitmap;
        CDrawView::ForceInvalidate();
    }
}